<html>
<head><meta charset="utf-8"><title>Missing (?) StorageLive/Dead statements? · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Missing.20(.3F).20StorageLive.2FDead.20statements.3F.html">Missing (?) StorageLive/Dead statements?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="205780594"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Missing%20%28%3F%29%20StorageLive/Dead%20statements%3F/near/205780594" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Edd Barrett <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Missing.20(.3F).20StorageLive.2FDead.20statements.3F.html#205780594">(Aug 03 2020 at 10:44)</a>:</h4>
<p>Hi,</p>
<p>I'd always assumed that the compiler would insert <code>StorageLive</code>/<code>StorageDead</code> statements into MIR when a variable comes in and out of "scope".</p>
<p>In <a href="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=98271135fe85b9e0e60c5706ddc30818">this example</a>  however, the result of the calls to <code>CheckedAdd</code> are never marked live or dead.</p>
<p>I was wondering why that is?</p>
<p>(Why am I asking? In our Rust JIT, we were hoping to use <code>StorageLive/Dead</code> as hints to the register allocator).</p>
<p>Thanks!</p>
<p><a href="/user_uploads/4715/6lQw54zVWeaK_iaam0AmXUK0/image.png">image.png</a></p>
<div class="message_inline_image"><a href="/user_uploads/4715/6lQw54zVWeaK_iaam0AmXUK0/image.png" title="image.png"><img src="/user_uploads/4715/6lQw54zVWeaK_iaam0AmXUK0/image.png"></a></div>



<a name="205780723"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Missing%20%28%3F%29%20StorageLive/Dead%20statements%3F/near/205780723" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Missing.20(.3F).20StorageLive.2FDead.20statements.3F.html#205780723">(Aug 03 2020 at 10:46)</a>:</h4>
<p>temporaries might not get them</p>



<a name="205781271"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Missing%20%28%3F%29%20StorageLive/Dead%20statements%3F/near/205781271" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Missing.20(.3F).20StorageLive.2FDead.20statements.3F.html#205781271">(Aug 03 2020 at 10:54)</a>:</h4>
<p>Also on the cleanup path no variables get <code>Storage{Live,Dead}</code>.</p>



<a name="205781351"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Missing%20%28%3F%29%20StorageLive/Dead%20statements%3F/near/205781351" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Missing.20(.3F).20StorageLive.2FDead.20statements.3F.html#205781351">(Aug 03 2020 at 10:55)</a>:</h4>
<p>we're considering scrapping these annotations entirely and computing them on the fly for each statement</p>



<a name="205781851"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Missing%20%28%3F%29%20StorageLive/Dead%20statements%3F/near/205781851" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Edd Barrett <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Missing.20(.3F).20StorageLive.2FDead.20statements.3F.html#205781851">(Aug 03 2020 at 11:03)</a>:</h4>
<p>Thanks for your replies!</p>
<p>It's looking less likely that we can rely on <code>StorageLive</code>/<code>StorageDead</code> for our (niche) purposes.</p>
<p>I've just had a prod around in the compiler. It's not clear to me how a temporary is different from short-lived local. Is there a definition somewhere?</p>



<a name="205782794"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Missing%20%28%3F%29%20StorageLive/Dead%20statements%3F/near/205782794" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Missing.20(.3F).20StorageLive.2FDead.20statements.3F.html#205782794">(Aug 03 2020 at 11:17)</a>:</h4>
<p>you can rely on them, but if they aren't there, you need to assume the thing lives for the entire function</p>



<a name="205782812"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Missing%20%28%3F%29%20StorageLive/Dead%20statements%3F/near/205782812" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Missing.20(.3F).20StorageLive.2FDead.20statements.3F.html#205782812">(Aug 03 2020 at 11:18)</a>:</h4>
<p>this is how miri does it</p>



<a name="205782895"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Missing%20%28%3F%29%20StorageLive/Dead%20statements%3F/near/205782895" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Missing.20(.3F).20StorageLive.2FDead.20statements.3F.html#205782895">(Aug 03 2020 at 11:19)</a>:</h4>
<p><a href="https://github.com/rust-lang/rust/blob/dbc2ef25fb5e15445de38f19ba75547a6cf35cae/src/librustc_mir/interpret/eval_context.rs#L642">https://github.com/rust-lang/rust/blob/dbc2ef25fb5e15445de38f19ba75547a6cf35cae/src/librustc_mir/interpret/eval_context.rs#L642</a></p>



<a name="205782922"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Missing%20%28%3F%29%20StorageLive/Dead%20statements%3F/near/205782922" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Edd Barrett <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Missing.20(.3F).20StorageLive.2FDead.20statements.3F.html#205782922">(Aug 03 2020 at 11:19)</a>:</h4>
<p>I see. Thanks.</p>
<p>And when does the compiler use these temporaries?</p>



<a name="205803712"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Missing%20%28%3F%29%20StorageLive/Dead%20statements%3F/near/205803712" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Missing.20(.3F).20StorageLive.2FDead.20statements.3F.html#205803712">(Aug 03 2020 at 14:56)</a>:</h4>
<p>I don't understand the question. Are you asking when the code being compiled uses these temporaries? That would be "anywhere within the function, we don't know unless we check the body"</p>



<a name="205805427"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Missing%20%28%3F%29%20StorageLive/Dead%20statements%3F/near/205805427" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Edd Barrett <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Missing.20(.3F).20StorageLive.2FDead.20statements.3F.html#205805427">(Aug 03 2020 at 15:07)</a>:</h4>
<p>In other wording: when does the compiler opt to use a temporary variable instead of a regular local variable? It's not clear to me the difference between a temporary variable and a normal one.</p>
<p>Thanks!</p>



<a name="205806236"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Missing%20%28%3F%29%20StorageLive/Dead%20statements%3F/near/205806236" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Missing.20(.3F).20StorageLive.2FDead.20statements.3F.html#205806236">(Aug 03 2020 at 15:14)</a>:</h4>
<p>There is not really a difference on the MIR level. It just uses a "internal" variables that aren't present in surface Rust to implement checked arithmetic and drop flags, and those don't get storage statements (maybe there's a few other cases?).</p>



<a name="205811061"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Missing%20%28%3F%29%20StorageLive/Dead%20statements%3F/near/205811061" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Missing.20(.3F).20StorageLive.2FDead.20statements.3F.html#205811061">(Aug 03 2020 at 15:51)</a>:</h4>
<p>bodies of constants and statics also don't use <code>StorageDead</code> and <code>StorageLive</code></p>



<a name="205811253"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Missing%20%28%3F%29%20StorageLive/Dead%20statements%3F/near/205811253" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Missing.20(.3F).20StorageLive.2FDead.20statements.3F.html#205811253">(Aug 03 2020 at 15:52)</a>:</h4>
<p>I'm pretty sure they do? I saw a comment in rustc claiming they didn't, but <code>--emit=mir</code> says otherwise.</p>



<a name="205811471"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Missing%20%28%3F%29%20StorageLive/Dead%20statements%3F/near/205811471" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Edd Barrett <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Missing.20(.3F).20StorageLive.2FDead.20statements.3F.html#205811471">(Aug 03 2020 at 15:54)</a>:</h4>
<blockquote>
<p>variables that aren't present in surface Rust</p>
</blockquote>
<p>^ Ah ha! That's the detail I was missing. Thanks!</p>



<a name="205818226"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Missing%20%28%3F%29%20StorageLive/Dead%20statements%3F/near/205818226" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Missing.20(.3F).20StorageLive.2FDead.20statements.3F.html#205818226">(Aug 03 2020 at 16:51)</a>:</h4>
<p>ah, then it's probably just all the temporary variables that go into the return value of a constant or static that don't have storage statements, but there it makes a lot of sense, because these values are leaked to static memory</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>